package com.db.williamchart.renderer.executor;

import com.db.williamchart.Painter;
import com.db.williamchart.data.AxisType;
import com.db.williamchart.data.Frame;
import com.db.williamchart.data.Label;

import java.util.ArrayList;
import java.util.List;

public class DebugWithLabelsFrame {

    public List<Frame> invoke(Painter painter,
                              AxisType axisType,
                              List<Label> xLabels,
                              List<Label> yLabels,
                              Float labelsSize) {

        float ascent = painter.measureLabelAscent(labelsSize);
        float descent = painter.measureLabelDescent(labelsSize);
        List<Frame> labelsFrames = new ArrayList<>();

        if (axisType.shouldDisplayAxisX()) {
            for (Label label : xLabels) {
                float labelHalfWidth = painter.measureLabelWidth(label.getLabel(), labelsSize) / 2;
                Frame frame = new Frame(
                        label.getScreenPositionX() - labelHalfWidth,
                        label.getScreenPositionY() + ascent,
                        label.getScreenPositionX() + labelHalfWidth,
                        label.getScreenPositionY() + descent
                );
                labelsFrames.add(frame);
            }
        }

        if (axisType.shouldDisplayAxisY()) {
            for (Label label : yLabels) {
                float labelHalfWidth = painter.measureLabelWidth(label.getLabel(), labelsSize) / 2;
                Frame frame = new Frame(
                        label.getScreenPositionX() - labelHalfWidth,
                        label.getScreenPositionY() + ascent,
                        label.getScreenPositionX() + labelHalfWidth,
                        label.getScreenPositionY() + descent
                );
                labelsFrames.add(frame);
            }
        }

        return labelsFrames;
    }
}
